namespace Core.Base
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using Contracts.Enums;
    using Contracts.Interfaces;
    using System.Linq;

    /// <summary>
    /// Ammunition base class
    /// </summary>
    public abstract class AmmunitionBase : IAmmunition, IEquatable<AmmunitionBase>, IEquatable<IAmmunition>
    {
        private readonly IModifier[] _modifiers;
        private readonly string _name;
        private readonly string _description;
        private readonly AmmunitionSizesEnum _size;
        private readonly AmmunitionGroupesEnum _group;
        private readonly AmmunitionTypesEnum _type;
        private readonly IEnumerable<LazyFeature> _lazyFeatures;

        /// <summary>
        /// Create a new instance of the <see cref="T:Core.Base.AmmunitionBase"/> class.
        /// </summary>
        protected AmmunitionBase(){}
        /// <summary>
        /// Create a new instance of the <see cref="T:Core.Base.AmmunitionBase"/> class.
        /// </summary>
        protected AmmunitionBase(string name, string description, AmmunitionSizesEnum size, AmmunitionGroupesEnum group, AmmunitionTypesEnum type, IEnumerable<LazyFeature> features, IModifier[] modifiers)
        {
            if (string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");
            if (size == AmmunitionSizesEnum.NotDefined) throw new InvalidEnumArgumentException("size");
            if (group == AmmunitionGroupesEnum.NotDefined) throw new InvalidEnumArgumentException("group");
            if (type == AmmunitionTypesEnum.NotDefined) throw new InvalidEnumArgumentException("type");

            _name = name;
            _description = description;
            _size = size;
            _group = group;
            _type = type;
            _lazyFeatures = features ?? new LazyFeature[]{};
            _modifiers = modifiers;
        }

        /// <summary>
        /// Obtains or defines the features
        /// </summary>
        public virtual IEnumerable<IFeature<object>> Features
        {
            get {  return GetFeatures(); }
        }
        /// <summary>
        /// Determines if therre is any features
        /// </summary>
        public virtual bool HasFeatures
        {
            get { return _lazyFeatures.Any(); }
        }
        /// <summary>
        /// Obtains or defines the ammunition modifiers
        /// </summary>
        public virtual IEnumerable<IModifier> Modifiers
        {
            get { return _modifiers; }
        }
        /// <summary>
        /// Determines if there is any modifier with IsVisible set to true.
        /// </summary>
        public virtual bool HasVisibleModifiers
        {
            get { return Modifiers != null && Modifiers.Where(m => m.IsVisible).Any(); }
        }

        /// <summary>
        /// Obtains the ammunition name
        /// </summary>
        public virtual string Name
        {
            get { return _name; }
        }

        /// <summary>
        /// Obtains the ammunition description
        /// </summary>
        public virtual string Description
        {
            get { return _description; }
        }

        /// <summary>
        /// Obtains the ammunition size
        /// </summary>
        public virtual AmmunitionSizesEnum Size
        {
            get { return _size; }
        }

        /// <summary>
        /// Obtains the ammunition group
        /// </summary>
        public virtual  AmmunitionGroupesEnum Group
        {
            get { return _group; }
        }

        /// <summary>
        /// Obtains the ammunition type
        /// </summary>
        public virtual AmmunitionTypesEnum Type
        {
            get { return _type; }
        }

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(AmmunitionBase other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other._name, _name);
        }

        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(IAmmunition other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Equals(other.Name, _name);
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param><filterpriority>2</filterpriority>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (AmmunitionBase)) return false;
            return Equals((AmmunitionBase) obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type. 
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override int GetHashCode()
        {
            return base.GetHashCode() ;
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override string ToString()
        {
            return _name;
        }

        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        /// A new object that is a copy of this instance.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public object Clone()
        {
            return Activator.CreateInstance(this.GetType());
        }

        /// <summary>
        /// Obtains the ammunition features
        /// </summary>
        /// <param name="predicate">An optional predicate to filter result</param>
        /// <returns></returns>
        private IEnumerable<IFeature<object>> GetFeatures(Predicate<IFeature<object>> predicate = null)
        {
            using (var enu = _lazyFeatures.GetEnumerator())
                while (enu.MoveNext())
                    if(predicate ==null)
                        yield return enu.Current.Feature;
                    else if (predicate(enu.Current.Feature))
                        yield return enu.Current.Feature;
        }
    }
}